package org.springhbn.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import org.springhbn.domain.Forum;


@Repository("forumDao")
public class ForumDao extends BaseDao implements ForumDaoInterface{

	@Override
	public void addForum(Forum forum) {
		getHibernateTemplate().save(forum);
	}

	@Override
	public void updateForum(Forum forum) {
		getHibernateTemplate().update(forum);
	}

	@Override
	public Forum getForum(int forumId) {
		return getHibernateTemplate().get(Forum.class,forumId);
	}

	@Override
	public List<Forum> findForumByName(String name) {
		return (List<Forum>)getHibernateTemplate().find("from Forum f where f.forumName like ?",name + "%");
	}

	@Override
	public long getForumNum() {
	 return	(Long) getHibernateTemplate().iterate("select count(f.forumId) from Forum f").next();
	}
	
	public long getForumNum2() {
		Long forumNum = getHibernateTemplate().execute(new HibernateCallback<Long>() {

			@Override
			public Long doInHibernate(Session session)
					throws HibernateException, SQLException {
				return (Long) session.createQuery("select count(f.forumId) from Forum f").list().iterator().next();
			}
		});
		return forumNum;
	}

	
	
}
